package main

import "fmt"

type Ojb interface {
	SendMsg(msg string)
	RevMsg(msg string)
}

type Mediator interface {
	Register(user User)
	SendMsg(msg string, user Ojb)
}

type User struct {
	Name     string
	Mediator Mediator
}

func (u User) SendMsg(msg string) {
	fmt.Printf("用户 %s 发送消息 %s\n", u.Name, msg)
	u.Mediator.SendMsg(msg, u)
}
func (u User) RevMsg(msg string) {
	fmt.Printf("用户 %s 接收消息 %s\n", u.Name, msg)
}

type ChatRoom struct {
	Users []User
}

func (c *ChatRoom) Register(user User) {
	c.Users = append(c.Users, user)
}

func (c *ChatRoom) SendMsg(msg string, user Ojb) {
	for _, u := range c.Users {
		if u != user {
			u.RevMsg(msg)
		}
	}
}

func main() {
	room := &ChatRoom{}
	u1 := User{Mediator: room, Name: "张三"}
	u2 := User{Mediator: room, Name: "李四"}
	u3 := User{Mediator: room, Name: "王五"}
	room.Register(u1)
	room.Register(u2)
	room.Register(u3)

	u1.SendMsg("你好呀")
	u2.SendMsg("吃饭了吗")
	u3.SendMsg("我吃了")
}
